<?php
/**
 * @author GhostWei
 * @date 2012-7-16
 */
class controller_www_special extends front_controller_panel {

	private $special = null;
	public function init() {
		parent::init();
		$name = trim($this->_param('name'));
		$this->view->special = $this->special = $this->cache()->special()->getInformation(array('name' => $name, 'related' => 10));
		if(!$this->special) {
			throw new front_exception($this->language('该专辑不存在'), 404);
		}
	}
	
	public function action_html() {
		$this->action_gallery();
	}
	
	public function action_gallery() {		
		$firstPhotoId = 0;
		foreach($this->special['_photos'] AS $key => $row) {
			if($key == 0) {
				$firstPhotoId = $row['id'];
				break;
			}
		}
		$this->getRequest()->setParam('id', $firstPhotoId);
		$this->getRequest()->setActionName('photo');
		$this->action_photo();
	}
	
	public function action_index() {
		$this->setHeadMetas();
		$page = system_nav_page::create($this->special['title'])->setRoute('special/index')->setParams(array('name' => $this->special['name']));
		$this->_crumbs()->addPage($page);
		$this->render();
	}
	
	public function action_photo() {
		$id = intval($this->_param('id'));
		$photo = null;
		foreach($this->special['_photos'] AS $row) {
			if($row['id'] == $id) {
				$photo = $row;
				break;
			}
		}
		if(!$photo) {
			throw new front_exception($this->language('该图片不存在'), 404);
		}
		$this->setHeadMetas();
		$this->view->photo = $photo;
		$title = $this->special['title'];
		$this->_crumbs()->addPage(system_nav_page::create($title)->setRoute('special/photo')->setParams(array('name' => $this->special['name'], 'id' => $id)));
		$this->layout()->setLayout('layout/photo');
		$this->render();
	}
	
	private function setHeadMetas() {
		$keywords = array();
		foreach((array) $this->special['_keywords'] AS $kw) {
			$keywords[] = $kw['name'];
		}
		$description = array();
		if($this->special['description']) {
			$description[] = $this->special['description'];
		}
		$description[] = $this->special['title'];
		$this->view->headKeywords = implode(',', $keywords);
		$this->view->headDescription = implode(', ', $description);
	}
	
}
